#include "StdAfx.h"
#include "MyControler.h"
#include "MyNetwork.h"
#include "DlgMain.h"
#include "user.h"

#define TEST_IP _T("172.16.10.17")
#define TEST_PORT 18888

CMyController::CMyController(void)
{
	m_pNetwork = new CMyNetwork;
	m_pUser = new CUser;
}

CMyController::~CMyController(void)
{
	if (m_pNetwork)
	{
		m_pNetwork->Close();
		delete m_pNetwork;
		m_pNetwork = NULL;
	}
}

void CMyController::Login(HWND hWnd, LPCTSTR lpszUserName, LPCTSTR lpszPassword)
{
	_ASSERT(m_pNetwork);
	m_hLoginWnd = hWnd;
	if (m_pNetwork&&!m_pNetwork->IsConnected())
		m_pNetwork->Init(this, TEST_IP, TEST_PORT);

	if (m_pNetwork->IsConnected())
	{
		char buf[256] = {0};
		int len = 0;
		CMyProtocol mp;
		USES_CONVERSION;
		if (m_pUser)m_pUser->SetSelfName(T2A(lpszUserName));
		mp.Login_write(buf, len, T2A(lpszUserName), T2A(lpszPassword));

		m_pNetwork->Send(buf, len);
	}
}

void CMyController::Logout(HWND hWnd)
{
	_ASSERT(m_pNetwork);
	m_hLoginWnd = hWnd;
	if (m_pNetwork&&!m_pNetwork->IsConnected())
		m_pNetwork->Init(this, TEST_IP, TEST_PORT);

	if (m_pNetwork->IsConnected())
	{
		char buf[256] = {0};
		int len = 0;
		CMyProtocol mp;
		USES_CONVERSION;
		mp.Logout_write(buf, len, m_uid);

		m_pNetwork->Send(buf, len);
	}
}


void CMyController::Register(HWND hWnd, LPCTSTR lpszUserName, LPCTSTR lpszPassword/*, ... */)
{
	_ASSERT(m_pNetwork);
	m_hLoginWnd = hWnd;
	if (m_pNetwork&&!m_pNetwork->IsConnected())
		m_pNetwork->Init(this, TEST_IP, TEST_PORT);

	if (m_pNetwork->IsConnected())
	{
		char buf[256] = {0};
		int len = 0;
		CMyProtocol mp;
		USES_CONVERSION;
		mp.Register_write(buf, len, T2A(lpszUserName), T2A(lpszPassword));

		m_pNetwork->Send(buf, len);
	}

}

void CMyController::TermServer(HWND hWnd)
{
	_ASSERT(m_pNetwork);
	m_hLoginWnd = hWnd;
	if (m_pNetwork&&!m_pNetwork->IsConnected())
		m_pNetwork->Init(this, TEST_IP, TEST_PORT);

	if (m_pNetwork->IsConnected())
	{
		char buf[256] = {0};
		int len = 0;
		CMyProtocol mp;
		USES_CONVERSION;
		mp.Term_write(buf, len);

		m_pNetwork->Send(buf, len);
	}
}

void CMyController::SendMsg(HWND hWnd, int toUser, int fromUser, LPCTSTR lpszMsg)
{
	_ASSERT(m_pNetwork);
	m_hLoginWnd = hWnd;
	if (m_pNetwork&&!m_pNetwork->IsConnected())
		m_pNetwork->Init(this, TEST_IP, TEST_PORT);

	if (m_pNetwork->IsConnected())
	{
		char buf[256] = {0};
		CMyProtocol mp;
		int len = 0;
		USES_CONVERSION;
		mp.Message_write(buf, len, toUser, fromUser, T2A(lpszMsg));

		m_pNetwork->Send(buf, len);
	}
}

void CMyController::SendMsgToAll(HWND hWnd, int fromUser, LPCTSTR lpszMsg)
{
	SendMsg(hWnd, 0, fromUser, lpszMsg);
}

void CMyController::SendData(HWND hWnd, int type, LPCTSTR lpszP1, LPCTSTR lpszP2, LPCTSTR lpszP3, LPCTSTR lpszP4)
{
	_ASSERT(m_pNetwork);
	m_hLoginWnd = hWnd;
	if (m_pNetwork&&!m_pNetwork->IsConnected())
		m_pNetwork->Init(this, TEST_IP, TEST_PORT);

	switch (type)
	{
	case 0:	// Register
		// type|user|pwd
		Register(hWnd, lpszP1, lpszP2);
		break;
	case 1:	// Login
		// type|user|pwd
		Login(hWnd, lpszP1, lpszP2);
		break;
	case 2:	// Send Message
		// type|to_user|from_user|MESSAGE.
		SendMsg(hWnd, _ttol(lpszP1), _ttol(lpszP2), lpszP3);
		break;
	case 3:
		// type|MESSAGE
		SendMsgToAll(hWnd, _ttol(lpszP1), lpszP2);
		break;
	case 4:
		// type
		TermServer(hWnd);
		break;
	case 5:
		Logout(hWnd);
		break;
	default:
		break;
	}
	
}

void CMyController::OnResponse(unsigned char type, char* res, int len)
{
	CByteStream bs((unsigned char*)res);
	switch (type)
	{
	case RNPT_LOGIN:
		{
			int uid = 0;
			bs>>uid;
			if (m_pUser)
			{
				_userinfo *ui = NULL;
				m_pUser->GetSelfInfo(ui);
				ui->_uid = uid;
				m_pUser->AddUser(ui);
			}

			//SendMessage(m_hLoginWnd, WM_RESPONSE, m_uid, 0);
			PostMessage(m_hLoginWnd, WM_RESPONSE, m_uid, 0);
		}
		break;
	case RNPT_LOGOUT:
		{
			SendMessage(m_hLoginWnd, WM_RESPONSE, 1, 0);
		}
		break;
	case RNPT_REGISTRY:
//		break;
	case RNPT_MESSAGE:
//		break;
	case RNPT_MESSAGE_ALL:
//		break;
	default:
		SendMessage(m_hLoginWnd, WM_RESPONSE, 1, 1);
		break;
	}
	
}

CUser* CMyController::GetUsers()
{
	return m_pUser;
}
